package CarPackage;

import GoodsPackage.Good;
import GoodsPackage.GoodsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/deletecar.do")
public class DeleteCarServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            HttpSession session = request.getSession();
            String id = request.getParameter("id");
            Object user_id = session.getAttribute("user_id");

            CarDao carDao = new CarDao();
            int res = carDao.removeFromCar(id, user_id.toString());
            if (res > 0) {
                System.out.println("删除成功");
            }

            List<Car> carList = carDao.getCarByUserId(user_id.toString());
            request.getSession().setAttribute("carList", carList);
            response.sendRedirect("car.jsp");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
